<%@page import="classes.AccountManager"%>
<%@page import="classes.SubjectManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.apache.commons.io.FilenameUtils"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileUploadException"%>
<%@page import="java.io.InputStream"%>
<%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="java.io.FileOutputStream"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>



<%
try {
	String subject = "";
	ArrayList<String> lectureres = new ArrayList<String>();
	String url = "C:\\Users\\Davidlek\\workspace\\Project\\HomeWorkManager\\WebContent\\sylabus\\";
	boolean empty = false;
    List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
    for (FileItem item : items) {
        if (item.isFormField()) {
            // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
            String fieldname = item.getFieldName();
            String fieldvalue = item.getString();
            
            if(fieldname.equals("subjectName")){
            	if(fieldvalue.isEmpty()){
            		out.println("Subject name is empty. please write subject name. ");
            		out.println("<a href=\"addSubject.jsp\">try again</a>");
            		empty = true;
            	}
            	else{
            		subject = fieldvalue;
            	}
            }
            if(fieldname.equals("lecturers")){
            	if(fieldvalue.isEmpty()){
            		out.println("Lecturer name is empty. please write subject name. ");
            		out.println("<a href=\"addSubject.jsp\">try again</a>");
            		empty = true;
            	}
            	else{
            		lectureres.add(fieldvalue.substring(0,fieldvalue.indexOf(' ')));
            	}
            }
            
        } else {
            // Process form file field (input type="file").
            String fieldname = item.getFieldName();
            String filename = FilenameUtils.getName(item.getName());
            if(filename.isEmpty()){
            	out.println("you havent choosed file to upload. please ");
            	out.println("<a href=\"addSubject.jsp\">try again.</a>");
            	out.println("<br>");
            	empty = true;
            }
            else{
				            
            	InputStream filecontent = item.getInputStream();
            	url += filename;
           		FileOutputStream fos = new FileOutputStream(url);//TODO: url
            	int i;
            	while((i = filecontent.available()) > 0){
            		byte[] b = new byte[i];
            		filecontent.read(b,0,b.length);                
            		fos.write(b);  
            	}
            	filecontent.close();  
            	fos.flush();  
            	fos.close(); 
            	}
		}
    }
    
    
    if(!empty){
    	SubjectManager subManager = (SubjectManager)application.getAttribute(SubjectManager.SUBJECTMANAGER);
   		
   		subManager.createSubject(subject, url);//TODO: url
   		
   		int subjectID = subManager.getSubjectId(subject);
    	SubjectManager.setSyllabusUrl(subjectID, url);
    	
   		for(String lecturer : lectureres){
    		SubjectManager.addLecturersToSubject(subjectID, lecturer);
   		}
    	out.println("subject added.");
    	out.println("<br>");
    	out.println("<a href=\"subjects.jsp\">add another subject</a>");
    	out.println("<br>");
    	out.println("<a href=\"adminLogin.jsp\">go back</a>");
    }
    
} catch (FileUploadException e) {
    throw new ServletException("Cannot parse multipart request.", e);
}





%>

</body>
</html>